package com.example.ylsys.plants.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "database.db";

    public static final String TABLE_LOGIN = "login";
    public static final String TABLE_PLANTS="plants";
    public static final String TABLE_APIJSON="apijson";

    //创建 表的 sql 语句
    public static final String LOGIN_CREATE_TABLE_SQL =
            "create table login("
            + "id integer primary key autoincrement,"
            + "tel_number varchar(11) not null,"
            + "secret_code varchar(200) not null"
            + ");";

    public static final String PLANTS_CREATE_TABLE_SQL =
            "create table plants("
                    + "id integer primary key autoincrement,"
                    + "PictureUrl varchar(200) not null,"
                    + "PlantName varchar(20) not null,"
                    + "PlantBodyStort varchar(20) not null,"
                    + "Date varchar(100) not null,"
                    + "Describe varchar(200),"
                    + "Longitude varchar(100) not null,"
                    + "Latitude varchar(100) not null,"
                    + "IsUpload boolean not null default 0"
                    + ");";
    public static final String APIJSON_CREATE_TABLE_SQL =
            "create table apijson("
                    + "id integer primary key autoincrement,"
                    + "LabelName varchar(20) not null,"
                    + "JsonContent not null"
                    + ");";

    public MyDatabaseHelper(Context context, int DB_VERSION) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 在这里通过 db.execSQL 函数执行 SQL 语句创建所需要的表
        // 创建 students 表
        db.execSQL(LOGIN_CREATE_TABLE_SQL);
        db.execSQL(PLANTS_CREATE_TABLE_SQL);
        db.execSQL(APIJSON_CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 数据库版本号变更会调用 onUpgrade 函数，在这根据版本号进行升级数据库
        switch (oldVersion) {
            case 1:

                break;

            default:
                break;
        }
    }
    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            // 启动外键
            db.execSQL("PRAGMA foreign_keys = 1;");

//            //或者这样写
//            String query = String.format("PRAGMA foreign_keys = %s", "ON");
//            db.execSQL(query);
        }
    }


}
